package com.lancoo.answer.view.fragment.shortAnswer;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lancoo.answer.R;
import com.lancoo.answer.model.bean.Child;
import com.lancoo.answer.model.bean.Item;
import com.lancoo.answer.widget.combinationView.BriefAnswerView;

import java.util.List;

/**
 * Created by pengmi on 2022/7/7 14:27
 * Description:一题多空的简答题适配器
 */
public class ShortAnswerAdapter extends RecyclerView.Adapter<ShortAnswerAdapter.VH> {

    private final List<Item> itemList;
    private final int answerType;
    private final String typeNo,genreID;
    private int currentAnswerIndex;
    private final String TAG_LOCAL_UPDATE="request_focus";
    private final String TAG_STOP_ANSWER="close_audio_anwer";


    public ShortAnswerAdapter(Child child,String typeNo,String genreID){

        Log.e("ShortAnswerAdapter","typeNo:"+typeNo);
        itemList=child.getItemList();
        answerType= child.getChildAnswerType();
        this.typeNo= typeNo;
        this.genreID= genreID;
    }


    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.ev_list_item_short_answer,
                parent, false);
        return new VH(inflate,answerType,typeNo,genreID);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        holder.initData(itemList.get(position), position);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position, @NonNull List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
        if(payloads.contains(TAG_LOCAL_UPDATE)){
            Log.e("eee","去更新啦");
            holder.requestFocus();
        } else if(payloads.contains(TAG_STOP_ANSWER)){
            holder.closeAudioAnswer();
        }
    }


    @Override
    public int getItemCount() {
        return itemList.size();
    }

    protected static class VH extends RecyclerView.ViewHolder{

        private final int answerType;
        private final String typeNo,genreID;
        private final BriefAnswerView briefAnswerView;
        private Item item;

        public VH(@NonNull View itemView, int answerType,String typeNo,String genreID) {
            super(itemView);
            this.answerType=answerType;
            this.typeNo=typeNo;
            this.genreID=genreID;
            briefAnswerView = itemView.findViewById(R.id.bref_answer_view);
        }

        private void initData(Item item,int position){
            this.item=item;
            briefAnswerView.intData(item,answerType);
            briefAnswerView.setChildAnswerType(4);
            briefAnswerView.setDistinguishType(0);
            if(position==0&&TextUtils.equals(genreID,"107")){
                briefAnswerView.setDistinguishType(1);
            }else{
                briefAnswerView.setDistinguishType(0);
            }
            briefAnswerView.setChildIndex(position);
            briefAnswerView.setOpenMicroponeAnswer(!TextUtils.equals(typeNo,"G")||TextUtils.equals(typeNo,"g"));
            briefAnswerView.setCallBack(new BriefAnswerView.OnFocusCallBack() {
                @Override
                public void updateCallBack(int seletIndex) {
                    item.setFouceIndex(seletIndex);
                }

                @Override
                public void updateAnswerIndex(int childIndex) {
                    RecyclerView.Adapter<? extends RecyclerView.ViewHolder> bindingAdapter = getBindingAdapter();
                    if(bindingAdapter instanceof ShortAnswerAdapter){
                        ((ShortAnswerAdapter)bindingAdapter).updateCurrentAnswerIndex(childIndex);
                    }
                }
            });
            briefAnswerView.adjustFontSize();
        }

        private void requestFocus(){
            Log.e("ee","重新进行光标定位");
            briefAnswerView.requestEditTextFocus();
            int selecIndex=item.getFouceIndex();
            if(selecIndex!=-1){
                briefAnswerView.setFocusIndex(selecIndex);
            }
        }

        /**
         * 强制关闭音频作答
         */
        private void closeAudioAnswer(){
            briefAnswerView.closeAudioAnswer();
        }
    }

    public void closeAudioAnswer(){
        notifyItemChanged(currentAnswerIndex,TAG_STOP_ANSWER);
    }

    public void requestFocus(){
        notifyItemChanged(currentAnswerIndex,TAG_LOCAL_UPDATE);
    }


    public void updateCurrentAnswerIndex(int position){
        Log.e("shortAnswerAdapter","updateCurrentAnswerIndex："+position);
        currentAnswerIndex=position;
    }

    public int getCurrentAnswerIndex(){
        return currentAnswerIndex;
    }

}
